#!/usr/bin/env python3
# 检查新闻分类分布情况

import mysql.connector
from collections import Counter
import sys
import os

# 添加当前目录到路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from intelligent_classifier import news_classifier

# 数据库配置
DB_CONFIG = {
    'host': 'localhost',
    'user': 'root',
    'password': '962464',
    'database': 'news_aggregator',
    'charset': 'utf8mb4'
}

def check_category_distribution():
    """检查当前分类分布"""
    try:
        conn = mysql.connector.connect(**DB_CONFIG)
        cursor = conn.cursor(dictionary=True)
        
        # 获取分类统计
        cursor.execute("""
            SELECT 
                COALESCE(category, 'general') as category,
                COUNT(*) as count,
                ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM news), 2) as percentage
            FROM news 
            GROUP BY COALESCE(category, 'general')
            ORDER BY count DESC
        """)
        
        results = cursor.fetchall()
        total_news = sum(r['count'] for r in results)
        
        print(f"📊 新闻分类分布统计 (总计: {total_news} 条)")
        print("=" * 60)
        
        for result in results:
            category = result['category']
            count = result['count']
            percentage = result['percentage']
            category_name = news_classifier.get_category_name(category)
            
            print(f"{category_name:8} ({category:12}): {count:5} 条 ({percentage:5.1f}%)")
        
        # 检查综合类占比
        general_count = next((r['count'] for r in results if r['category'] == 'general'), 0)
        general_percentage = (general_count / total_news * 100) if total_news > 0 else 0
        
        print("\n" + "=" * 60)
        if general_percentage > 50:
            print(f"⚠️  警告: 综合类占比过高 ({general_percentage:.1f}%)，需要优化分类算法")
        elif general_percentage > 30:
            print(f"⚡ 注意: 综合类占比较高 ({general_percentage:.1f}%)，建议继续优化")
        else:
            print(f"✅ 分类效果良好: 综合类占比 ({general_percentage:.1f}%) 在合理范围内")
        
        # 获取最近的一些新闻样本来分析
        cursor.execute("""
            SELECT title, content, category, source_id
            FROM news 
            WHERE category = 'general' OR category IS NULL
            ORDER BY created_at DESC 
            LIMIT 10
        """)
        
        general_samples = cursor.fetchall()
        
        if general_samples:
            print(f"\n📋 最近归类为'综合'的新闻样本 ({len(general_samples)}条):")
            print("-" * 60)
            for i, sample in enumerate(general_samples, 1):
                title = sample['title'][:50] + '...' if len(sample['title']) > 50 else sample['title']
                print(f"{i:2}. {title}")
                
                # 尝试重新分类这条新闻
                predicted_category = news_classifier.classify_news(
                    title=sample['title'],
                    content=sample['content'] or '',
                    source_name=''
                )
                if predicted_category != 'general':
                    predicted_name = news_classifier.get_category_name(predicted_category)
                    print(f"    💡 建议分类: {predicted_name} ({predicted_category})")
                print()
        
        cursor.close()
        conn.close()
        
    except Exception as e:
        print(f"❌ 检查分类分布失败: {e}")

if __name__ == "__main__":
    check_category_distribution()